import moment from 'moment';

const getForm = (vueComponent) => {
    const _this = vueComponent;
    const form = _this.$refs.formModal
        ? _this.$refs.formModal.$refs.formGroup.form
        : _this.$refs.formPreview.$refs.formGroup.form;
    return form;
};

const getValueByAliasName = (vueComponent, aliasName) => {
    const form = getForm(vueComponent);
    const _this = vueComponent;
    let value;
    _this.formOptions.forEach((option, optIdx) => {
        option.rows.forEach((row, rowIdx) => {
            if (row.children) {
                row.children.forEach((item) => {
                    if (item.aliasName === aliasName && !value) {
                        value = form[`${option.inputName}_${optIdx}_${rowIdx}_${item.inputName}`];
                    }
                });
            }
        });
    });
    return value;
};

const getValueByInputName = (vueComponent, inputName) => {
    const form = getForm(vueComponent);
    let value;
    Object.keys(form).forEach((prop) => {
        if (prop.indexOf(inputName) > -1) {
            value = form[prop];
        }
    });
    return value;
};

// 强保单
const dateBeforeReportDate = (rule, value, callback, vueComponent) => {
    const dateValue = getValueByAliasName(vueComponent, 'reportDate');
    if (dateValue) {
        if (moment(dateValue) < moment(value)) {
            callback('强保日期不能晚于上报日期');
        } else {
            callback();
        }
    } else {
        callback('请先选择上报日期!');
    }
};

const buyDateBefore = (rule, value, callback, vueComponent) => {
    // 强保日期 yrevmar
    const dateKey = 'yrevmar';
    const dateValue = getValueByInputName(vueComponent, dateKey);
    if (dateValue) {
        if (moment(dateValue) < moment(value)) {
            callback('购车日期不能晚于强保日期');
        } else {
            callback();
        }
    } else {
        callback();
    }
};

const buyDateAfter = (rule, value, callback, vueComponent) => {
    // 生产日期 yrevmar
    const dateKey = 'wrekdez';
    const dateValue = getValueByInputName(vueComponent, dateKey);
    if (dateValue) {
        if (moment(value) < moment(dateValue)) {
            callback('购车日期不能早于生产日期');
        } else {
            callback();
        }
    } else {
        callback();
    }
};

// 外出救急单
const startTimeValidate = (rule, value, callback, vueComponent) => {
    // 到达时间
    const dateKey = 'hixwrln';
    const dateValue = getValueByInputName(vueComponent, dateKey);
    if (dateValue) {
        if (moment(dateValue) < moment(value)) {
            callback('出发时间不能大于到达时间');
        } else {
            callback();
        }
    } else {
        callback();
    }
};

const endTimeValidate = (rule, value, callback, vueComponent) => {
    // 出发时间
    const dateKey = 'zjemgwx';
    const dateValue = getValueByInputName(vueComponent, dateKey);
    if (dateValue) {
        if (moment(dateValue) > moment(value)) {
            callback('到达时间不能小于出发时间');
        } else {
            callback();
        }
    } else {
        callback();
    }
};

const buyCarDate = (rule, value, callback, vueComponent) => {
    const reportDate = getValueByAliasName(vueComponent, 'reportDate');
    const produceDate = getValueByAliasName(vueComponent, 'produceDate');
    if (reportDate) {
        if (moment(reportDate) < moment(value)) {
            callback('购车日期不能晚于上报日期');
        }
        if (moment(value) < moment(produceDate)) {
            callback('购车日期不能早于生产日期');
        }
        callback();
    } else {
        callback('请先选择上报日期!');
    }
};

export default {
    dateBeforeReportDate,
    buyDateBefore,
    buyDateAfter,
    startTimeValidate,
    endTimeValidate,
    buyCarDate
};
